<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>
<!DOCTYPE html>
<html lang="en">
<head>
<base href="<%=basePath%>">
<!-- 下拉框 -->
<link rel="stylesheet" href="static/ace/css/chosen.css" />
<!-- jsp文件头和头部 -->
<%@ include file="../index/top.jsp"%>
<!-- 日期框 -->
<link rel="stylesheet" href="static/ace/css/datepicker.css" />
<style type="text/css">
	td{    vertical-align: inherit !important;}
</style>
</head>
<body class="no-skin">

	<!-- /section:basics/navbar.layout -->
	<div class="main-container" id="main-container">
	<div id="zhongxin" style="padding-top: 13px;"></div>
		<!-- /section:basics/sidebar -->
		<div class="main-content">
			<div class="main-content-inner">
				<div class="page-content">
					<div class="row">
						<div class="col-xs-12">
						<!-- 检索  -->
						<form action="period/toGoodsSpecTime.do" method="post" name="goodsForm" id="goodsForm">
                        <input type="hidden" id="timeId" name="timeId" value="${pd.timeId }">
						<table id="simple-table" class="table table-striped table-bordered table-hover" >
							<thead>
								<tr>
                                    <th class="center">ID</th>
                                    <th class="center">期次标题</th>
                                    <th class="center">基地</th>
                                    <th class="center">库存</th>
                                    <th class="center">零售价</th>
                                    <th class="center">截止使用时间</th>
                                    <th class="center">上架时间</th>
                                    <th class="center">下架时间</th>
                                    <th class="center">状态</th>
                                    <th class="center">操作</th>
								</tr>
							</thead>
							<tbody id="goodlist">
							<!-- 开始循环 -->	
							<c:choose>
								<c:when test="${not empty specTime}">
									<c:if test="${QX.cha == 1 }">
									<c:forEach items="${specTime}" var="goods" varStatus="vs">
										<tr>
                                            <td class="center">${goods.id}</td>
                                            <td class="center">${goods.title}</td>
                                            <td class="center">${goods.name}</td>
                                            <td class="center">${goods.stock_num}</td>
                                            <td class="center">${goods.price}</td>
                                            <td class="center">${goods.end_use_time}</td>
                                            <td class="center">${goods.start_time}</td>
                                            <td class="center">${goods.end_time}</td>
                                            <td class="center"><c:if test="${goods.state=='01'}">已上架</c:if><c:if test="${goods.state=='00'}">已下架</c:if></td>
                                            <td class="center">
                                              <c:if test="${QX.edit == 1 }">
                                               <c:if test="${goods.state=='00' }"> 
                                                  <a class="btn btn-xs btn-success" title="编辑" onclick="editPeriod('${goods.id}');">
                                                    <i class="ace-icon fa fa-pencil-square-o bigger-120" title="编辑"></i>
                                                  </a>
                                                  <a class="btn btn-xs" title="上架" onclick="editGoodsState('${goods.id}','01','${goods.end_time }')" >
                                                  <i class="ace-icon fa fa-lock bigger-120" title="上架"></i>
                                                  </a>
                                                </c:if>
                                                <c:if test="${goods.state =='01' }">
                                                  <a class="btn btn-xs btn-success" title="下架"
                                                    onclick="editGoodsState('${goods.id}','00','${goods.end_time }')"> <i
                                                    class="ace-icon fa fa-unlock bigger-120"
                                                    title="下架"></i>
                                                  </a>
                                                </c:if>
                                                </c:if>
                                              </td>
										</tr>
									
									</c:forEach>
									</c:if>
									<c:if test="${QX.cha == 0 }">
										<tr>
											<td colspan="11" class="center">您无权查看</td>
										</tr>
									</c:if>
								</c:when>
								<c:otherwise>
									<tr class="main_info">
										<td colspan="11" class="center">没有相关数据</td>
									</tr>
								</c:otherwise>
							</c:choose>
							</tbody>
						</table>
    					<div class="page-header position-relative">
    					<table style="width:100%;">
    						  <tr>
                                  <td style="vertical-align:top;"><div class="pagination" style="float: right;padding-top: 0px;margin-top: 0px;">${page.pageStr}</div></td>
    						  </tr>
    					</table>
    					</div>
    					</form>
	
						</div>
						<!-- /.col -->
					</div>
					<!-- /.row -->
				</div>
				<!-- /.page-content -->
			</div>
		</div>
		<!-- /.main-content -->

		<!-- 返回顶部 -->
		<a href="#" id="btn-scroll-up" class="btn-scroll-up btn btn-sm btn-inverse">
			<i class="ace-icon fa fa-angle-double-up icon-only bigger-110"></i>
		</a>

	</div>
	<!-- /.main-container -->

	<!-- basic scripts -->
	<!-- 页面底部js¨ -->
	<%@ include file="../index/foot.jsp"%>
	<!-- 删除时确认窗口 -->
	<script src="static/ace/js/bootbox.js"></script>
	<!-- ace scripts -->
	<script src="static/ace/js/ace/ace.js"></script>
	<!-- 日期框 -->
	<script src="static/ace/js/date-time/bootstrap-datepicker.js"></script>
	<!-- 下拉框 -->
	<script src="static/ace/js/chosen.jquery.js"></script>
	<!--提示框-->
	<script type="text/javascript" src="static/js/jquery.tips.js"></script>
	</body>

<script type="text/javascript">
$(top.hangge());

//检索
function searchs(){
	top.jzts();
}

function editPeriod(specTimeId){
	 top.jzts();
	 var diag = new top.Dialog();
	 diag.Drag=true;
	 diag.Title ="商品规格期次";
	 diag.URL = '<%=basePath%>period/toGoodsSpecEdit.do?specTimeId='+specTimeId;
	 diag.Width = 1100;
	 diag.Height = 700;
	 diag.CancelEvent = function(){ //关闭事件
		 if(diag.innerFrame.contentWindow.document.getElementById('zhongxin').style.display == 'none'){
			 debugger
			 if('${page.currentPage}' == '0'){
				 top.jzts();
				 setTimeout("self.location=self.location",100);
			 }else{
				 nextPage('${page.currentPage}');
			 }
			}
			diag.close();
		 };
		 diag.show();	
 }
 

function toGoodsSpec(specId){
	 top.jzts();
	 var diag = new top.Dialog();
	 diag.Drag=true;
	 diag.Title ="修改规格";
	 diag.URL = '<%=basePath%>goods/toSpecEdit.do?specId='+specId;
	 diag.Width = 1200;
	 diag.Height = 700;
	 diag.CancelEvent = function(){ //关闭事件
		 if(diag.innerFrame.contentWindow.document.getElementById('zhongxin').style.display == 'none'){
			 debugger
			 alert("111")
			 if('${page.currentPage}' == '0'){
				 top.jzts();
				 setTimeout("self.location=self.location",100);
			 }else{
				 nextPage('${page.currentPage}');
			 }
		}
		diag.close();
	 };
	 diag.show();
}

function toDecimal(x) {    
    var f = parseFloat(x);    
    if (isNaN(f)) {    
        return;    
    }    
    f = Math.round(x*100)/100;    
    return f;    
} 

//修改上下架状态
function editGoodsState(id,state,endTime){
	$.get("<%=basePath%>period/updateSpecTimeState.do",{
		         specTimeId:  id,
		        state:   state,
		        endTime: endTime,
		        },function(data){
		        //这里显示从服务器返回的数据
		       if("success"==data){
		    	   alert("修改成功");
		       }else if("failed"==data){
		    	   alert("修改失败");
		       }else if("timeEnd"==data){
		    	   alert("此商品下架时间已到!");
		       }else if("no-stock"==data){
		    	   alert("期次库存到达上限!");
		       }else if("no-state"==data){
		    	   alert("区域商品未上架!");
		       }else if("no-goods"==data){
		    	   alert("本期次还未绑定商品!");
		       }else if("no-time"==data){
		    	   alert("商品参数不全!");
		       }else if("userTimeEnd"==data){
		    	   alert("商品截止使用时间已到!");
		       }
		        nextPage('${page.currentPage}');
		    });	
}

//复选框全选控制
var active_class = 'active';
$('#simple-table > thead > tr > th input[type=checkbox]').eq(0).on('click', function(){
	var th_checked = this.checked;//checkbox inside "TH" table header
	$(this).closest('table').find('tbody > tr').each(function(){
		var row = this;
		if(th_checked) $(row).addClass(active_class).find('input[type=checkbox]').eq(0).prop('checked', true);
		else $(row).removeClass(active_class).find('input[type=checkbox]').eq(0).prop('checked', false);
	});
});

//批量上架
function batchUpState(){
			var str='';
			var state='';
			for(var i=0;i < document.getElementsByName('ids').length;i++)
			{
				  if(document.getElementsByName('ids')[i].checked){
					  if(document.getElementsByName('ids')[i].id !='00'){
							alert("存在已上架规格");
							return false;
						}
				  	if(str==''){
				  		str += document.getElementsByName('ids')[i].value;
				  	}else {
				  		str += ',' + document.getElementsByName('ids')[i].value;
				  	}
				  }
			} 
		 	if(str==''){
				alert("您没有选择任何规格");
				return false;
			}else{ 
					$.ajax({
						type: "POST",
						url: '<%=basePath%>goods/updateSpecStateList.do?tm='+new Date().getTime(),
				    	data: {state:'01',ids:str},
						success: function(data){
						    if("success"==data){
							  alert("批量上架成功");
						   }else if("timeEnd"==data.substring(0,7)){
							   alert("id为(  "+data.substring(7,data.length)+"  )的商品规格下架时间已到，无法上架");
						   }else{
							   alert("批量上架失败");
						   }
							nextPage('${page.currentPage}');
							 
						}
					});
				}
		}
		
//批量下架
function batchDownState(){
	var str='';
	var state='';
	for(var i=0;i < document.getElementsByName('ids').length;i++)
	{
		  if(document.getElementsByName('ids')[i].checked){
			  if(document.getElementsByName('ids')[i].id!='01'){
					alert("存在已下架规格");
					return false;
				}
		  	if(str=='') str += document.getElementsByName('ids')[i].value;
		  	else str += ',' + document.getElementsByName('ids')[i].value;
		  }
	} 
 	if(str==''){
		alert("您没有选择任何规格");
		return false;
	}else{ 
			$.ajax({
				type: "POST",
				url: '<%=basePath%>goods/updateSpecStateList.do?tm='+new Date().getTime(),
		    	data: {state:'00',ids:str},
				success: function(data){
					if("success" == data){
						 alert("批量下架成功");
					}else{
						 alert("批量下架失败");
					}
					nextPage('${page.currentPage}');
					 
				}
			});
		}
}

function nextPage(page){ top.jzts();	if(true && document.forms[0]){
	var url = document.forms[0].getAttribute("action");
	if(url.indexOf('?')>-1){url += "&currentPage=";}
	else{url += "?currentPage=";}
	url = url + page + "&showCount=10";
	document.forms[0].action = url;
	document.forms[0].submit();
	}else{
		var url = document.location+'';
		if(url.indexOf('?')>-1){
			if(url.indexOf('currentPage')>-1){
				var reg = /currentPage=\d*/g;
				url = url.replace(reg,'currentPage=');
			}else{
				url += "&currentPage=";
			}
		}else{url += "?currentPage=";}
		url = url + page + "&showCount=10";
		document.location = url;
	}
}
</script>
</html>
